//
//  DSTabBar.m
//  weibo
//
//  Created by wudongyang on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DSTabBar.h"
#import "UIView+DSSealFrame.h"
#import "CenterViewController.h"
@interface DSTabBar ()
//中心按钮
@property(nonatomic, retain) UIButton *CenterButton;

@end

@implementation DSTabBar

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // 给tabBar 一个背景图片
        //        [self setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
        
        // 创建按钮取名(红色加号:RedAddButton)
        UIButton *CenterButton = [UIButton buttonWithType:UIButtonTypeCustom];
        // 按钮未点击状态 图片
        [CenterButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        // 按钮点击状态下 高亮模式
        [CenterButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        // 让红色加号按钮大小等于图片大小
        CenterButton.size = CenterButton.currentBackgroundImage.size;
        
        [self addSubview:CenterButton];
        self.CenterButton = CenterButton;
        [CenterButton addTarget:self action:@selector(CenterButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.width;
    CGFloat height = self.height;
    
    
    // 让红色加号按钮在中心
    self.CenterButton.center = CGPointMake(width * 0.5, height * 0.5);
    
    // 设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    // 一个按钮的宽度 5分之1
    CGFloat buttonW = width / 5;
    // 高度占据整个tabBar的高度
    CGFloat buttonH = height;
    // 索引为0
    NSInteger index = 0;
    for (UIView *button in self.subviews) {
        //        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        // 如果不是继承UIControl就排除掉 或者你是RedAddButton也排除掉
        if (![button isKindOfClass:[UIControl class]] || button == self.CenterButton) continue;
        
        // 计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        // 增加索引
        index++;
    }
}
// 红色按钮的点击方法
-(void)CenterButtonAction:(UIButton *)button{
    NSLog(@"点击方法");
    self.myBlock();
   
    
    
}
@end
